#Include ..\_lib\GdipStarter.ahk
#Include ..\_lib\Extend_Merged.ahk
#Include ..\_lib\Tip.ahk
#Include ..\_lib\Cursor.ahk
#Include ..\_lib\MouseEvent.ahk

#Include zoom.ahk
#Include hook.ahk
#Include save.ahk
#Include paste.ahk
#Include util.ahk
#Include ui/frameGui.ahk
#Include ui/baseGui.ahk
#Include ui/pasteGui.ahk
#Include ui/fullScreenGui.ahk
#Include ui/historyGui.ahk
#Include ui/delayGui.ahk
#Include ui/toolBarGui.ahk
#Include ui/regionGui.ahk

#Include ../ui/Settings.ahk

isReady := true

tray := A_TrayMenu
  , tray.delete()
  , tray.add(_t('tray.c'), (*) => Start((*) => Sleep(300)))
  , tray.add(_t('tray.dc'), (*) => Start((*) => DelayGui.Show(3)))
  , tray.add(_t('tray.l'), (*) => PasteHistory())
  , tray.add(_t('tray.of'), (*) => PasteFile(FileSelect(, cfg.defaultSavePath)))
  , tray.add()
  , tray.add(_t('tray.g'), TrayGroup())
  , tray.add()
  , tray.add(_t('tray.s'), (*) => Settings().Show())
  , tray.add(_t('tray.od'), (*) => Run(A_ScriptDir))
  , tray.add()
  , tray.add(_t('tray.r'), (*) => Reload())
  , tray.add(_t('tray.e'), (*) => ExitApp())
  , tray.ClickCount := cfg.clickCount
  , tray.Default := _t('tray.c')

OnError((*) => (ClearAllGui(), 0)) ; 报错时关闭所有gui

Hotkey cfg.clipHK, (*) => Start()
Hotkey cfg.clearAllHK, (*) => ClearAll()
HotKey cfg.lastHK, (*) => PasteHistory()

TrayGroup() {
  m := Menu(), ['history', cfg.groupsList*].foreach(v => m.Add(v, _SwitchGroup))
  lastChecked := custom.Get('active', 'history'), m.Check(lastChecked)
  m.Add(), m.Add('select', _SelectOne)
  return m

  _SwitchGroup(_name, *) {
    m.Uncheck(lastChecked), m.Check(_name), lastChecked := _name
    HistoryGui.FlushCache(), History.Init(cfg.groupRoot '\' _name)
    custom.Set('active', _name), custom.Sync()
  }

  _SelectOne(_name, *) {
    if !r := DirSelect(cfg.groupRoot, 0)
      return
    HistoryGui.FlushCache(), History.Init(r)
    m.Uncheck(lastChecked), m.Check(_name), lastChecked := _name
  }
}

Start(Before := unset) {
  global isReady
  if !isReady
    return
  isReady := false, IsSet(Before) && Before()
  Tip.ShowTip(''), StaticBG.Show(), cfg.showMaskIST && Mask.Show()
  Cursor.SetIcon(Cursor.Icon.cross)

  cancelCB := Region.GetRegion()
  Hotkey(cfg.cancelHK, _cancel, 'On'), Hotkey('RButton Up', _cancel, 'On')

  _cancel(*) {
    cancelCB()
    ResetState()
  }
}

ClearAllGui() {
  for w in WinGetList('ahk_pid ' ProcessExist()) {
    if IsSelfGui(w)
      GuiFromHwnd(w).Destroy()
  }
}

ClearAll(excludes := []) {
  list := []
  for w in WinGetList('ahk_pid ' ProcessExist()) {
    if (g := GuiFromHwnd(w)) is FullScreenGui
      return
    if g is BaseGui and !excludes.findIndex(v => v = g)
      list.Push(g)
  }
  if !list.Length
    return
  for v in list
    v.Destroy()
  History.Reset()
}

AutoSave(g) {
  if !cfg.autoSave
    return
  logger.Debug('自动保存截图')
  g.savePath := f := SaveToFileEx(g.Hwnd, cfg.groupRoot '\' custom.Get('active'))
  id := History.AddFile(f), HistoryGui.FlushCache(), g.id := id
}

ResetState() {
  HotKeysOff(cfg.cancelHK, 'RButton Up', 'LButton', 'Left', 'Right', 'Up', 'Down'
    , '^Left', '^Right', '^Up', '^Down', 'MButton'
    , '+Left', '+Right', '+Up', '+Down', '``')
  StaticBG.Close(), Mask.Close(), Cursor.SetIcon(Cursor.Icon.arrow), Tip.ShowTip('')
  global isReady := true
}